﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Common
{
    public class PageSql
    {
        public static String GetPage(PageTable page)
        {

            return string.Format(
            "select  * From {2} where {3} {4} limit {1},{0}",
                page.PageCount, page.PageCount * (page.CurrPage - 1), page.Table, page.Where, page.Order, page.NotID);
        }
        /// <summary>
        /// 指定想查询的字段，一般用在多表查询
        /// </summary>
        /// <param name="filed"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public static String GetPage(String filed, PageTable page)
        {
            if (page.CurrPage == 1)
            {
                return string.Format(
                "select top {0} {6} From {2} where {3} and {5} {4}",
                    page.PageCount, page.PageCount * (page.CurrPage - 1), page.Table, page.Where, page.Order, page.NotID, filed);
            }
            else
            {
                return string.Format(
                "select top {0} {6} From {2} where {3} and {5} not in(select top {1} {5} From {2} where {3} {4}) {4}",
                    page.PageCount, page.PageCount * (page.CurrPage - 1), page.Table, page.Where, page.Order, page.NotID, filed);
            }

        }
    }

    public class PageTable
    {
        public int PageCount;//每页显示数量
        public int CurrPage;//当前页码
        public String Table;//表名
        public String Where;//条件
        public String Order;//排序
        public String NotID;//是用哪个字段来分页的，一般是主键FID，默认不修改
    }

}